//
//  STPageViewController.swift
//  iPhone
//
//  Created by Toj on 11/17/22.
//

import UIKit

protocol STPageContentViewProtocol: NSObjectProtocol { }

extension UIView: STPageContentViewProtocol { }
extension UIViewController: STPageContentViewProtocol { }

protocol STPageViewControllerDelegate: NSObjectProtocol {
    func pageViewController(_ vc: STPageViewController, didSelectAt index: Int, didTapTitle title: String)
    func pageViewController(_ vc: STPageViewController, didSelectedAt index: Int, didTapTitle title: String)
}
 
extension STPageViewControllerDelegate {
    func pageViewController(_ vc: STPageViewController, didSelectAt index: Int, didTapTitle title: String) { }
    func pageViewController(_ vc: STPageViewController, didSelectedAt index: Int, didTapTitle title: String) { }
}

// MARK: - 懒加载(属性), 变量, 初始化init
class STPageViewController: UIViewController {
    
    weak var pdelegate: STPageViewControllerDelegate?
    
    private(set) var viewStyle = STPageViewStyle()
    
    public var selectedIndex = 0 { didSet {
        guard titles.count > selectedIndex,
              views.count > selectedIndex
        else { return }
        titleView.selectedIndex = selectedIndex
        contentView.selectedIndex = selectedIndex
    } }
    
    public var titles: [String] = [] { didSet {
        titleView.titles = titles
        titleView.selectedIndex = selectedIndex
    } }
    
    public var views: [STPageContentViewProtocol] = [] { didSet {
        contentView.views = views
        contentView.selectedIndex = selectedIndex
    } }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupMakeInitialize()
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
    }
    
    private lazy var titleView: STPageTitleView = {
        let view = STPageTitleView()
        view.delegate = self
        return view
    }()
    
    private lazy var contentView = STPageContentView()
    
    deinit {
        print("-->startimes: \(st_className) func: deinit")
    }
}

// MARK: - 代理事件 STPageTitleViewDelegate
extension STPageViewController: STPageTitleViewDelegate {
    
    func titleView(_ view: STPageTitleView, didSelect index: Int) {
        let title = view.currentTitle
        pdelegate?.pageViewController(self, didSelectAt: index, didTapTitle: title)
    }
    
    func titleView(_ view: STPageTitleView, didSelected index: Int, title: String) {
        contentView.titleSelected(at: index)
        pdelegate?.pageViewController(self, didSelectedAt: index, didTapTitle: title)
    }
}

// MARK: - 初始化设置
private extension STPageViewController {
    
    func setupMakeAddSubviews() {
        addChild(contentView)
        view.addSubview(titleView)
        view.addSubview(contentView.view)
    }
    
    func setupMakeLayoutSubviews() {
        titleView.snp.makeConstraints { make in
            make.top.left.right.equalTo(view)
            make.height.equalTo(viewStyle.titleStyle.height)
        }
        
        contentView.view.snp.makeConstraints { make in
            make.top.equalTo(titleView.snp.bottom)
            make.bottom.left.right.equalTo(view)
        }
    }
    
    func setupMakeInitialize() {
        view.backgroundColor = viewStyle.backgroundColor
        
        titleView.viewStyle = viewStyle.titleStyle
        contentView.viewStyle = viewStyle.contentStyle
    }
}
